<?php
session_start();
ob_start();
include(dirname(__DIR__).'/includes/db_conn_new.inc');  
include('logincheck.php');

if($_SERVER['REQUEST_METHOD'] === 'POST')
{
	if((isset($_POST['username']) && $_POST['username'] != '') && (isset($_POST['password']) && $_POST['password'] != '')){
		$res = checkUser($_POST['username'],$_POST['password']); 
		if($res)
		{
			$application = getApplication($_SESSION['app_id']);
			if($application != false)
			{
				//echo trim($application['app_result']); echo '<br />'; echo trim('1.5 Not eligible'); exit;
				//if(trim($application['app_result']) === trim('1.5 Not eligible')){
				if(strcmp($application['app_result'], '1.5 Not eligible') == 0){
					$_SESSION['applicant_id'] = '';
					$_SESSION['display_name'] = '';
					$_SESSION['app_email_addr'] = '';					
					$_SESSION['CurrentContact_id'] = '';
					$_SESSION['app_id'] = '';
					
					$_SESSION['not_eligible'] = 'login failed'; 				
					header('location:index.php?login'); exit;
				}else{
					header('Location:index.php?dashboard'); exit;
				}
					
			}
			else
			{
				$_SESSION['applicant_id'] = '';
				$_SESSION['display_name'] = '';
				$_SESSION['app_email_addr'] = '';					
				$_SESSION['CurrentContact_id'] = '';
				$_SESSION['app_id'] = '';
				$_SESSION['msg'] = 'login failed'; 
				header('location:index.php?login'); exit;
			}
		}
		else
		{
			$_SESSION['msg'] = 'login failed'; 
			header('location:index.php?login'); exit;
		}
	}
	else
	{
		$_SESSION['msg'] = 'username & password must not empty, login failed'; 
		header('location:index.php?login'); exit;
	}
}
else
{
	
	header('location:index.php?login'); exit;
}

/*
if((isset($_POST['username']) && $_POST['username'] != '') && (isset($_POST['password']) && $_POST['password'] != '')){

$res = checkUser($_POST['username'],$_POST['password']); 
	if($res){
		
		if($application = getApplication($_SESSION['app_id'])!= false)
		{
			
			if($application['app_result'] != '1.5 Not eligible'){
				header('Location:index.php?dashboard'); exit;
			}else{
				$_SESSION['not_eligible'] = 'login failed'; 
				header('location:index.php?login'); exit;
			}
				
		}
		else{
				$_SESSION['not_eligible'] = 'login failed'; 
				header('location:index.php?login'); exit;
			}
			
		
		//header('Location:index.php?dashboard'); exit;
	}else{
		$_SESSION['msg'] = 'login failed'; 
		header('location:index.php?login'); exit;
	}
}
else
{
	$_SESSION['msg'] = 'username & password must not empty, login failed'; 
	header('location:index.php?login'); exit;
}
*/

	/*
					echo '<html><head></head><body><div class="modal fade" id="message" role="dialog" aria-labelledby="myModalLabel" aria-hidden="true" style="display:none">

<div class="modal-dialog"><div class="modal-content">
<div class="modal-header"><button type="button" class="close" data-dismiss="modal" aria-hidden="true">&times;</button><h4 class="modal-title" id="myModalLabel"><i class="fa fa-exclamation-triangle"></i>Apply Reloan</h4>
</div>

<div class="modal-body"> 
<div id="text" style="font-size:14px; font-family:Arial, Helvetica, sans-serif">
<p>"Unfortuantely, based on iDecision its appears you  are not currently eligible. Please reapply in 3 months time. Thank you."</p>
</div>

</div>

<div class="modal-footer"><button type="button" class="btn btn-default" data-dismiss="modal">Close</button></div>
</div>
</div>
</div>
</body>
<script type="text/javascript">
$("#message").dialog({width:580,title:"Lets get started",autoOpen:true,modal:true}); 
$("#message").dialog("open"); 
</script></html>';
*/
					//echo $_SESSION['not_eligible'] ; exit;
	ob_flush(); 
?>  